#include "parameter_common.h"
#include "parameter.h"

static common_config_t common_base_info = {
	.magic = PARAMETER_COMMON_MAGIC,
	.version = CONFIG_COMMON_PARAMETER_VERSION,
	.size = CONFIG_COMMON_PARAMETER_SIZE,
	.core_bit_map = COMMON_CORE_BIT_INFO,
};

static common_config_t  common_info_l;
static common_config_t const * const common_ptr = &common_info_l;

int common_info_config(FILE *const fp, pm_info_t const*pm_d)
{
    if(NULL == fp){
         printf("fp is point to NULL\n");
         return -1;
     }

	fseek(fp, PM_COMMON_OFFSET, 0);
	fwrite(&common_base_info, sizeof(common_base_info), 1, fp);
    return 0;
}

int check_common(FILE * const fp, uint64_t base)
{
	uint32_t magic;
	fseek(fp, base + PM_COMMON_OFFSET, 0);
	fread(&magic,sizeof(magic),1,fp);
	if(PARAMETER_COMMON_MAGIC != magic)
		return -1;
	else
		return 0;
}

int get_common_info(FILE *const fp, parameter_base_t const*p_base)
{
    if(NULL == fp){
		ERROR("fp is point NULL %s \n", __func__);
		return -1;
	}

	fseek(fp, p_base->addr + PM_COMMON_OFFSET, 0);
	fread(&common_info_l,sizeof(common_config_t),1,fp);

    if(PARAMETER_COMMON_MAGIC != common_ptr->magic){
		ERROR("can not get right magic 0x%x, %s \n", common_ptr->magic, __func__);
		return -1;
	}

	printf("=======================================\n");

	printf("COMMON info : \n");
	printf("version = 0x%x\n", common_ptr->version);

	printf("core 0: %s\n", IS_ON(common_ptr->core_bit_map & BIT(0)));
	printf("core 1: %s\n", IS_ON(common_ptr->core_bit_map & BIT(1)));
	printf("core 2: %s\n", IS_ON(common_ptr->core_bit_map & BIT(4)));
	printf("core 3: %s\n", IS_ON(common_ptr->core_bit_map & BIT(5)));
	printf("core 4: %s\n", IS_ON(common_ptr->core_bit_map & BIT(8)));
	printf("core 5: %s\n", IS_ON(common_ptr->core_bit_map & BIT(9)));
	printf("core 6: %s\n", IS_ON(common_ptr->core_bit_map & BIT(12)));
	printf("core 7: %s\n", IS_ON(common_ptr->core_bit_map & BIT(13)));

	printf("=======================================\n");

	return 0;
}

pm_list_fun_t common_fun = {
	.cfg_fnc = common_info_config,
	.ck_fnc = check_common,
	.prf_fnc = get_common_info,
};